//
//  RNOLPresentAnimationTransition.m
//  RongNiuOnline
//
//  Created by apple on 2018/5/21.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLPresentAnimationTransition.h"

@implementation RNOLPresentAnimationTransition

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.6f;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toVC.view];
    
    //屏幕可以呈现视图的frame
    CGRect targetFrame = [transitionContext finalFrameForViewController:toVC];
    //初始位置
    toVC.view.frame = CGRectOffset(targetFrame, 0, CGRectGetHeight(targetFrame));
    
    /// 加个蒙层
    CGFloat coverH = targetFrame.size.height*0.3;
    UIView *topCoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, targetFrame.size.width , targetFrame.size.height)];
    topCoverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.55];
    
    //动画后  不要全屏
    targetFrame.origin.y = coverH;
    targetFrame.size.height -= coverH;
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.125 usingSpringWithDamping: 0.8 initialSpringVelocity:0.65 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
        [containerView addSubview: topCoverView];
        [containerView insertSubview:topCoverView atIndex:0];
        toVC.view.frame = targetFrame;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
    
}

@end
